package com.huawei.hianalytics.ab.bc.ik.ab;

import android.content.Context;
import android.text.TextUtils;
import com.huawei.hianalytics.ab.ab.cd;
import com.huawei.hianalytics.ab.bc.de.fg;
import com.huawei.hianalytics.ab.bc.ik.cd.bc;
import com.huawei.hianalytics.ab.bc.kl.ef;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;

public class ab {
    private static ab ab;
    private static Map<String, Long> cd = new HashMap();
    private Context bc;
    private long de = 0;

    public static ab ab() {
        return bc();
    }

    private static void ab(String str, String str2, long j, long j2, JSONObject jSONObject, String str3, String str4) {
        String str5 = str3;
        String str6 = str;
        bc.ab().ab(new com.huawei.hianalytics.ab.bc.ik.bc.bc(str5, ef.ab(jSONObject, str2, String.valueOf(j2), str4), str6, "oper", j));
    }

    private static synchronized ab bc() {
        ab abVar;
        synchronized (ab.class) {
            if (ab == null) {
                ab = new ab();
            }
            abVar = ab;
        }
        return abVar;
    }

    private void bc(Context context) {
        String bc = com.huawei.hianalytics.ab.bc.bc.ab.bc(context);
        String bc2 = com.huawei.hianalytics.ab.bc.ij.ab.bc(context, "global_v2", "app_ver", "");
        com.huawei.hianalytics.ab.bc.ij.ab.ab(context, "global_v2", "app_ver", bc);
        com.huawei.hianalytics.ab.ab.bc.bc(bc);
        com.huawei.hianalytics.ab.ab.bc.cd(bc2);
        if (TextUtils.isEmpty(bc2)) {
            com.huawei.hianalytics.ab.bc.ef.ab.bc("HiAnalyticsEventServer", "app ver is first save!");
        } else if (!bc2.equals(bc)) {
            com.huawei.hianalytics.ab.bc.ef.ab.bc("HiAnalyticsEventServer", "the appVers are different!");
            ab().ab("", "alltype", bc2);
        }
    }

    private static void cd(String str, String str2, String str3, JSONObject jSONObject) {
        long j = 0;
        long currentTimeMillis = System.currentTimeMillis();
        long longValue = cd.containsKey(str) ? ((Long) cd.get(str)).longValue() : 0;
        long j2 = currentTimeMillis - longValue;
        if (longValue != 0) {
            j = j2;
        }
        ab(str, str3, currentTimeMillis, j, jSONObject, str2, "OnPause");
    }

    public void ab(Context context) {
        this.bc = context;
        bc(context);
        com.huawei.hianalytics.ab.bc.cd.ab.ab.ab().cd().cd(com.huawei.hianalytics.ab.bc.bc.ab.ab());
    }

    public void ab(String str, int i) {
        if (this.bc == null) {
            com.huawei.hianalytics.ab.bc.ef.ab.cd("HiAnalyticsEventServer", "onReport() null context or SDK was not init.");
            return;
        }
        com.huawei.hianalytics.ab.bc.ef.ab.bc("hmsSdk", "onReport: Before calling runtaskhandler()");
        ab(str, ef.ab(i), com.huawei.hianalytics.ab.ab.bc.fg());
    }

    public void ab(String str, int i, String str2, JSONObject jSONObject) {
        long currentTimeMillis = System.currentTimeMillis();
        if (2 == i) {
            currentTimeMillis = ef.ab("yyyy-MM-dd", currentTimeMillis);
        }
        bc.ab().ab(new com.huawei.hianalytics.ab.bc.ik.bc.bc(str2, jSONObject, str, ef.ab(i), currentTimeMillis));
    }

    public void ab(String str, int i, String str2, JSONObject jSONObject, long j) {
        new fg(str, ef.ab(i), str2, jSONObject.toString(), j).ab();
    }

    public void ab(String str, String str2) {
        if (cd.ij(str, str2)) {
            long currentTimeMillis = System.currentTimeMillis();
            if (currentTimeMillis - this.de > 30000) {
                com.huawei.hianalytics.ab.bc.ef.ab.ab("HiAnalyticsEventServer", "begin to call onReport!");
                this.de = currentTimeMillis;
                ab(str, str2, com.huawei.hianalytics.ab.ab.bc.fg());
                return;
            }
            com.huawei.hianalytics.ab.bc.ef.ab.cd("HiAnalyticsEventServer", "autoReport timeout. interval < 30s ");
            return;
        }
        com.huawei.hianalytics.ab.bc.ef.ab.bc("HiAnalyticsEventServer", "auto report is closed tag:" + str);
    }

    public void ab(String str, String str2, String str3) {
        if (cd.ik(str, str2)) {
            String ab = com.huawei.hianalytics.ab.bc.bc.cd.ab(this.bc);
            if (!"WIFI".equals(ab)) {
                com.huawei.hianalytics.ab.bc.ef.ab.bc("HiAnalyticsEventServer", "strNetworkType is :" + ab);
                return;
            }
        }
        bc.ab().ab(new com.huawei.hianalytics.ab.bc.ik.bc.cd(str, str2, str3));
    }

    public void ab(String str, String str2, String str3, JSONObject jSONObject) {
        if (this.bc == null) {
            com.huawei.hianalytics.ab.bc.ef.ab.cd("HiAnalyticsEventServer", "onPause null context or SDK was not init.");
            return;
        }
        cd(str, str2, str3, jSONObject);
        cd.put(str, Long.valueOf(0));
    }

    public void ab(String str, String str2, JSONObject jSONObject) {
        ab(str, "$AppOnPause", str2, jSONObject);
    }

    public void bc(String str, String str2, String str3, JSONObject jSONObject) {
        long currentTimeMillis = System.currentTimeMillis();
        cd.put(str, Long.valueOf(currentTimeMillis));
        ab(str, str3, currentTimeMillis, 0, jSONObject, str2, "OnResume");
    }

    public void bc(String str, String str2, JSONObject jSONObject) {
        bc(str, "$AppOnResume", str2, jSONObject);
    }
}
